# -*- coding:utf-8 -*-

from typing import TypeVar, Generic, Any
from pydantic import BaseModel, Field

# 定义一个 TypeVar，用于表示泛型类型
T = TypeVar('T', int, str, bool, dict, list, BaseModel)

class ResponseModel(BaseModel, Generic[T]):
    code: int = Field(default=200, description="状态码")
    success: bool = Field(default=True, description="是否成功")
    message: str = Field(default=True, description="信息")
    data: T = Field(default=None, description="数据")
    class Config:
        from_attributes = True

class Page(BaseModel, Generic[T]):
    pageNo: int = Field(default=1, description="页码")
    pageSize: int = Field(default=10, description="每页条数")
    totalPage: int = Field(default=1, description="总页数")
    totalRows: int = Field(default=0, description="总条数")
    rows: list[T] = Field(default=[], description="数据")
    class Config:
        from_attributes = True

class ResponsePage(ResponseModel, Generic[T]):
    data: Page[T] = Field(default=None, description="数据")
    class Config:
        from_attributes = True


def resp(success: bool, code: int, message: str, data: dict={})->ResponseModel:
    return ResponseModel(code=code, success=success, message=message, data=data)
    
def success(code: int=0, message: str="操作成功", data: dict={})->ResponseModel:
    return resp(True, code, message, data)
    
def success_page(page_no=1, page_size=10, total_page = 0, total_rows=0, data: list=[])->ResponseModel:
    data = Page.from_orm(dict(pageNo=page_no, pageSize=page_size, totalPage=total_page, totalRows=total_rows, rows=data))
    return ResponsePage(code=200, success=True, message="查询成功", data=data)
    
def fail(code: int=-1, message: str="操作失败", data: dict={})->ResponseModel:
    return resp(False, code, message, data)
